package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;

import com.iwhalecloud.bss.kite.cucc.service.attr.handler.WaterMeterReadingServiceAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author liaojing
 * NB物联网水务抄表服务-超声波--型号属性联动
 * @create 2022-01-19-10:01
 */
@Service
@AttrHandler(WaterMeterReadingServiceAttrHandler.class)
public class WaterMeterReadingServiceAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        //型号的属性值
        String attrValue = attr.getValue();
        String custId = data.getCustId();
        String prodInstId = inst.getInstId();
        String sceneInstId = data.getSceneInstId();

        // 结算价属性处理
        KiteAttr settleAttr = querySceneInstService.getInstAttr(custId, InstType.PROD_INST, prodInstId, "200004108");
        //销售价属性处理
        KiteAttr sellAttr = querySceneInstService.getInstAttr(custId, InstType.PROD_INST, prodInstId, "200004107");

        //主框架型号对应的结算价
        if (Objects.nonNull(settleAttr)) {
            //NB物联网水务抄表服务-超声波水表DN15-汇中：302元
            setHandleAttrValue("110010396", attrValue, inst, "200004108", "302", sceneInstId);
            //key:型号枚举值ID  newFieldName:结算价的属性编码   newValue:结算价的枚举值ID
            //NB物联网水务抄表服务-超声波水表DN20-汇中：313元
            setHandleAttrValue("110010397", attrValue, inst, "200004108", "313", sceneInstId);

            //NB物联网水务抄表服务-超声波水表DN25-汇中：325元
            setHandleAttrValue("110010398", attrValue, inst, "200004108", "325", sceneInstId);

            //NB物联网水务抄表服务-超声波水表DN15-阀控-汇中：360元
            setHandleAttrValue("110010399", attrValue, inst, "200004108", "360", sceneInstId);

            //NB物联网水务抄表服务-超声波水表DN20-阀控-汇中：370元
            setHandleAttrValue("110010400", attrValue, inst, "200004108", "370", sceneInstId);

            //NB物联网水务抄表服务-超声波水表DN25-阀控-汇中：393元
            setHandleAttrValue("110010401", attrValue, inst, "200004108", "393", sceneInstId);
        }

        //主框架型号对应的销售价
        if (Objects.nonNull(sellAttr)) {
            //NB物联网水务抄表服务-超声波水表DN15-汇中：332元
            setHandleAttrValue("110010396", attrValue, inst, "200004107", "332", sceneInstId);

            //NB物联网水务抄表服务-超声波水表DN20-汇中：344元
            setHandleAttrValue("110010397", attrValue, inst, "200004107", "344", sceneInstId);

            //NB物联网水务抄表服务-超声波水表DN25-汇中：358元
            setHandleAttrValue("110010398", attrValue, inst, "200004107", "358", sceneInstId);

            //NB物联网水务抄表服务-超声波水表DN15-阀控-汇中：396元
            setHandleAttrValue("110010399", attrValue, inst, "200004107", "396", sceneInstId);

            //NB物联网水务抄表服务-超声波水表DN20-阀控-汇中：407元
            setHandleAttrValue("110010400", attrValue, inst, "200004107", "407", sceneInstId);

            //NB物联网水务抄表服务-超声波水表DN25-阀控-汇中：432元
            setHandleAttrValue("110010401", attrValue, inst, "200004107", "432", sceneInstId);
        }
        // 联动同场景下其他产品的型号
        setOthProdInstMode(attrValue, inst, sceneInstId);
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
    private void setHandleAttrValue (String key, String oldValue, KiteInst inst, String newFieldName,
                                     String newValue, String sceneInstId ) {
       if(KiteStringUtils.isEqual(key,oldValue)){
           operateInstAttrService.setAttrValue(inst, newFieldName, newValue, sceneInstId, null);
       }
    }
    private void setOthProdInstMode(String value, KiteInst inst, String sceneInstId) {
        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(sceneInstId);
        kiteProdInstList.stream().forEach(prodInst ->{
            // 找到型号
            KiteAttr kiteAttr = prodInst.getAttr("200002772");
            if (Objects.nonNull(kiteAttr) && !KiteStringUtils.isEqual(inst.getInstId(), prodInst.getProdInstId())) {
                List<KiteAttrValue> kiteAttrValue = kiteAttr.getAttrValues();
                //value是主控板型号的型号相同时，联动一致
                //value是附加商品枚举值时联动
                List<KiteAttrValue> attrValues = kiteAttrValue.stream().filter(AttrValue -> KiteStringUtils.equalsAny(value,"110010396","110010397","110010398","110010399","110010400","110010401")).collect(Collectors.toList());
                if (KiteListUtils.isNotEmpty(attrValues)) {
                    //水表框架
                    if (KiteStringUtils.equalsAny(prodInst.getSpecId(), "63249657")) {
                        operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), value, sceneInstId, null);
                    }
                    //外壳等塑料件
                    if (KiteStringUtils.equalsAny(prodInst.getSpecId(), "63249708")) {
                        if (KiteStringUtils.isEqual(value, "110010396")) {
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110010403", sceneInstId, null);
                        }
                        if (KiteStringUtils.isEqual(value, "110010397")) {
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110010404", sceneInstId, null);
                        }
                        if (KiteStringUtils.isEqual(value, "110010398")) {
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110010405", sceneInstId, null);
                        }
                        if (KiteStringUtils.isEqual(value, "110010399")) {
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110010406", sceneInstId, null);
                        }
                        if (KiteStringUtils.isEqual(value, "110010400")) {
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110010407", sceneInstId, null);
                        }
                        if (KiteStringUtils.isEqual(value, "110010401")) {
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110010408", sceneInstId, null);
                        }
                    }
                    //NB物联网水务抄表服务-超声波水表主控板
                    if (KiteStringUtils.equalsAny(prodInst.getSpecId(), "63249660")) {
                        if (KiteStringUtils.isEqual(value, "110010396")) {
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110010409", sceneInstId, null);
                        }
                        if (KiteStringUtils.isEqual(value, "110010397")) {
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110010410", sceneInstId, null);
                        }
                        if (KiteStringUtils.isEqual(value, "110010398")) {
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110010411", sceneInstId, null);
                        }
                        if (KiteStringUtils.isEqual(value, "110010399")) {
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110010412", sceneInstId, null);
                        }
                        if (KiteStringUtils.isEqual(value, "110010400")) {
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110010413", sceneInstId, null);
                        }
                        if (KiteStringUtils.isEqual(value, "110010401")) {
                            operateInstAttrService.setAttrValue(prodInst, kiteAttr.getFieldName(), "110010414", sceneInstId, null);
                        }
                    }
                }
            }
        });
    }
}
